#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int Maxn=1e6+7;
int n;ll dep[Maxn];

int main(){
	freopen("trip.in","r",stdin);
	freopen("trip.out","w",stdout);
	
	cin>>n; dep[1]=0; ll ans=0;
	for(int i=2;i<n;i++){
		int p,v;
		cin>>p>>v;
		dep[i]=dep[p]+v; 
	}
	sort(dep+1,dep+n+1);
	cout<<dep[n]+dep[n-1];
	

	return 0;
}
/*
 7
 1 1
 1 1
 2 1
 2 1
 3 1
 3 1
*/
